C++ Coding Standards : 101 Rules, Guidelines, and Best Practices (9780132654425) by Sutter Herb; Alexandrescu Andrei & Andrei Alexandrescu

C++ Coding Standards : 101 Rules, Guidelines, and Best Practices (9780132654425) by Sutter Herb; Alexandrescu Andrei & Andrei Alexandrescu

Author:Sutter, Herb; Alexandrescu, Andrei & Andrei Alexandrescu
Language: eng
Format: epub
Publisher: Prentice Hall
Published: 2005-11-15T00:00:00+00:00


References

[C++TR104]

61. Don’t define entities with linkage in a header file

Summary

Repetition causes bloat: Entities with linkage, including namespace-level variables or functions, have memory allocated for them. Defining such entities in header files results in either link-time errors or memory waste. Put all entities with linkage in implementation files.

Discussion

While starting to use C++, we all learn quite quickly that header files like

are liable to cause link-time errors complaining of duplicate symbols as soon as such a header is included by more than one source file. The reason is simple: Each source file actually defines and allocates space for fudgeFactor and hello and foo’s body, and when the time comes to put it all together (linking), the linker is faced with several symbols bearing the same name and competing for visibility.

The solution is simple—put just the declarations in the header:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.